package com.lbs.jiepiaisi.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.activity.PlatformMainActivity;
import com.lbs.jiepiaisi.base.BaseFragment;
import com.lbs.jiepiaisi.base.ErrorException;
import com.lbs.jiepiaisi.base.ProgressObserver;
import com.lbs.jiepiaisi.bean.AccountLoginBean;
import com.lbs.jiepiaisi.bean.UserEntity;
import com.lbs.jiepiaisi.config.Api;
import com.lbs.jiepiaisi.utils.ButtonUtil;
import com.lbs.jiepiaisi.utils.Constant1;
import com.lbs.jiepiaisi.utils.IntentUtil;
import com.lbs.jiepiaisi.utils.RxSchedulers;
import com.lbs.jiepiaisi.utils.SpUtil;
import com.lbs.jiepiaisi.utils.ToastUtil;

/**
 *
 * 时间：2017/6/20 14:07
 * 作用：平台账号密码登录
 */

public class AccountLoginFragment extends BaseFragment {
    private UserEntity userEntity;
    private Button butlogin;
    private CheckBox cb_remember;
    private EditText edit_account_number;//账号
    private EditText edit_password;//密码
    //public SharePreferenceManager mSharePreference = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //EventBus.getDefault().register(this);
    }

    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_platform_account_login, null);
        //mSharePreference = SharePreferenceManager.getInstance(getContext());
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void getExtras(View view) {

    }

    @Override
    protected void findView(View view) {
        butlogin = (Button) view.findViewById(R.id.btn_platform_login);
        cb_remember = (CheckBox) view.findViewById(R.id.cb_remember);
        edit_account_number = (EditText) view.findViewById(R.id.edit_account_number);
        edit_password = (EditText) view.findViewById(R.id.edit_password);

        //记住账号密码操作
        String account = (String)SpUtil.get(Constant1.ACCOUNT,"null");
        String password = (String)SpUtil.get(Constant1.PASSWORD,"null");
       // String account_number = mSharePreference.getString(Constant.ACCOUNT_NUMBER, "null");
       // String password = mSharePreference.getString(Constant.ACCOUNT_PASSWORD, "null");
        if (account.equals("") && password.equals("")) {
            cb_remember.setChecked(false);
        } else {
            cb_remember.setChecked(true);
            if (account.equals("null") && password.equals("null")) {
                //取出来为空时不设置
                cb_remember.setChecked(false);
            } else {//不为空时设置
                //设置账号
                edit_account_number.setText(account);
                //设置密码
                edit_password.setText(password);
            }
        }
    }

    @Override
    protected void setListener(View view) {
        butlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ButtonUtil.isFastClick()){
                    String account = edit_account_number.getText().toString();
                    String password = edit_password.getText().toString();
                    if (account == null || account.length() == 0 || password == null || password.length() <= 4) {
                        ToastUtil.show("账号或密码错误");
                        return;
                    }

                    Api.getInstance().createService().accountLogin(account,password)
                            .compose(RxSchedulers.observableIO2Main(mContext))
                            .subscribe(new ProgressObserver<AccountLoginBean>(mContext,true) {

                                @Override
                                public void onSuccess(AccountLoginBean result) {
                                    Log.e("deptIdType","type"+result.getResult().getDept().getDeptId().getClass());
                                    ToastUtil.show("登录成功");
                                    // 存userid
                                    SpUtil.put(Constant1.USERID,result.getResult().getUserId());
                                    // 存部门id
                                    SpUtil.put(Constant1.DEPTID,result.getResult().getDept().getDeptId());
                                    // 存部门名称
                                    SpUtil.put(Constant1.DEPT_NAME,result.getResult().getDept().getDeptName());
                                    // 存部门id，每次登录通过用deptId给underlingDeptId赋值，切换下属账户时，同步存储
                                    SpUtil.put(Constant1.UNDERLING_DEPTID,result.getResult().getDept().getDeptId());
                                    // 存上级部门Id
                                    SpUtil.put(Constant1.SUPER_DEPTID,result.getResult().getDept().getParentDeptId());
                                    // 存账户
                                    SpUtil.put(Constant1.ACCOUNT,account);
                                    // 存密码
                                    SpUtil.put(Constant1.PASSWORD,cb_remember.isChecked() ? password : "");

                                    IntentUtil.gotoActivity(getActivity(), PlatformMainActivity.class, false);
                                }

                                @Override
                                public void onFailure(ErrorException e) {
                                    if (e.getCode() == 1) {
                                        ToastUtil.show("账号或密码错误,请重新登录");
                                    } else {
                                        Log.e("登录失败","登录失败");
                                    }
                                }
                            });
                }

//                if (ButtonUtil.isFastClick()) {
//                    String account_number = edit_account_number.getText().toString();
//                    String password = edit_password.getText().toString();
//                    if ((account_number == null || account_number.length() == 0) || (password == null || password.length() <= 4)) {
//                        Toast.makeText(mContext, "账号或密码错误", Toast.LENGTH_SHORT).show();
//                    } else {
//                        WaitingDialogControll.showWaitingDialog(getContext());
//                        userEntity = new UserEntity();
//                        userEntity.setAccount_number(edit_account_number.getText().toString());
//                        userEntity.setAccount_password(edit_password.getText().toString());
//
//                        OkGo.<String>post(UrlUtil.ACCOUNT_LOGIN_URL)
//                                .tag(this)
//                                .retryCount(3)
//                                .params("loginName", userEntity.getAccount_number())//设置参数名称和参数值
//                                .params("password", userEntity.getAccount_password())//设置参数名称和参数值
//                                .execute(new StringCallback() {
//                                    @Override
//                                    public void onStart(Request<String, ? extends Request> request) {
//                                        super.onStart(request);
//                                        WaitingDialogControll.showWaitingDialog(getActivity());
//                                    }
//
//                                    @Override
//                                    public void onSuccess(com.lzy.okgo.model.Response<String> response) {
//                                        WaitingDialogControll.dismissWaitingDialog();
//                                        /////
//                                        String jsonString = response.body();//返回结果
//                                        Log.e("Login==:", jsonString);
//                                        //JSONObject jsonObject = new JSONObject(jsonString);//转换结果
//                                        UserEntity userEntity = LoginAccountParser.parser(jsonString);//json解析
//                                        try {
//                                            WaitingDialogControll.dismissWaitingDialog();
//                                            int code = userEntity.getCode();
//                                            // 用户userid
//                                            int userid = userEntity.getUserId();
//                                            // 部门id
//                                            int deptId = userEntity.getDeptId();//部门id
//                                            // 上级部门Id
//                                            String parentDeptId = userEntity.getParentDeptId();//部门id
//                                            // 下级部门Id
////                                                    int childDeptId =
//                                            String deptName = userEntity.getDeptName();
//                                            //调用sp存储
//                                            //userid存入sp
//                                            mSharePreference.put(Constant.ACCOUNT_USERID, userid);//存入userid
//                                            //部门id存入sp
//                                            mSharePreference.put(Constant.ACCOUNT_DEPTID, deptId);//存入部门id
//                                            //部门名称存入名称
//                                            mSharePreference.put(Constant.ACCOUNT_DEPT_NAME, deptName);//存入部门名称
//                                            //部门id，每次登录通过用deptId给childDeptId赋值，切换下属账户时，同步存储
//                                            mSharePreference.put(Constant.ACCOUNT_CHILD_DEPTID, deptId);//
//                                            mSharePreference.put(Constant.ACCOUNT_PARENT_DEPTID,parentDeptId);// 存入上级部门Id
//                                            String account_number = edit_account_number.getText().toString();
//
//                                            mSharePreference.put(Constant.ACCOUNT_NUMBER, account_number);//存入账号
//                                            if (cb_remember.isChecked()) {
//                                                String password = edit_password.getText().toString();
//                                                mSharePreference.put(Constant.ACCOUNT_PASSWORD, password);//存入密码
//                                            } else {
//                                                mSharePreference.put(Constant.ACCOUNT_PASSWORD, "");
//                                            }
//
//                                            if (code == 0) {
//                                                Toast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT).show();
//                                                IntentUtil.gotoActivity(getActivity(), PlatformMainActivity.class, false);
//                                            } else if (code == 1) {
//                                                Toast.makeText(mContext, "账号或密码错误,请重新登录", Toast.LENGTH_SHORT).show();
//                                            } else if (code == 2) {
//                                                Toast.makeText(mContext, "联网失败", Toast.LENGTH_SHORT).show();
//                                            } else if (code == Constant.STATUS_NO_NET) {
//                                                Toast.makeText(mContext, "请检查网络", Toast.LENGTH_SHORT).show();
//                                            } else if (code == 300) {
//                                                Toast.makeText(mContext, "账号或密码错误,请重新登录", Toast.LENGTH_SHORT).show();
//
//                                            }
//                                        } catch (Exception e) {
//                                            e.printStackTrace();
//                                            Log.e("88888", "444:" + e);
//                                            Toast.makeText(mContext, "服务器异常", Toast.LENGTH_SHORT).show();
//                                        }
//
//                                    }
//
//                                    @Override
//                                    public void onError(com.lzy.okgo.model.Response<String> response) {
//                                        System.out.println(response);
//                                        WaitingDialogControll.dismissWaitingDialog();
//                                        Toast.makeText(mContext, "连接超时", Toast.LENGTH_SHORT).show();
//                                    }
//                                });
//                    }
//                }
            }
        });
    }
}